<?php
/*
	@author:tong
	email:jiantong912@gmail.com
	company:重庆致晓网络科技
*/

if(!defined('IN_UCHOME') || !defined('IN_ADMINCP')) {
	exit('Access Denied');
}
$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('app_moviecredit')." WHERE type='movie'");
$value = $_SGLOBAL['db']->fetch_array($query);
$_SGLOBAL[moviecredit]=$value['sendmovie'];
//权限
if(!$allowmanage = checkperm('managemovie')) {
	$_GET['uid'] = $_SGLOBAL['supe_uid'];//只能操作本人的
	$_GET['username'] = '';
}

//删除控制
if(submitcheck('deletesubmit')) {
	//var_dump($_POST['ids']);exit();
	include_once(S_ROOT.'./source/function_delete.php');
	if(!empty($_POST['ids']) && deletemovies($_POST['ids'])) {
		cpmessage('do_success', $_POST['mpurl']);
	} else {
		cpmessage('at_least_one_option_to_delete_movies', $_POST['mpurl']);
	}
}


$mpurl = 'admincp.php?ac=movie';

//显示分类
$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('app_movie_type')." ORDER BY sort ASC");
while ($typeresult = $_SGLOBAL['db']->fetch_array($query)) {
	$typelist[] = $typeresult;
}
//var_dump($typelist);
$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('app_movie_region')." ORDER BY sort ASC");
while ($regionresult = $_SGLOBAL['db']->fetch_array($query)) {
	$regionlist[] = $regionresult;
}
//var_dump($regionlist);



//处理搜索
$intkeys = array('uid', 'movieid','typeid','regionid','checked');
$strkeys = array('username');
$randkeys = array(array('sstrtotime','dateline'));
$likekeys = array('mname');
$results = getwheres($intkeys, $strkeys, $randkeys, $likekeys);
$wherearr = $results['wherearr'];
//var_dump($wherearr);

$wheresql = empty($wherearr)?'1':implode(' AND ', $wherearr);
$mpurl .= '&'.implode('&', $results['urls']);

//排序
$orders = getorders(array('dateline'), 'movieid');
$ordersql = $orders['sql'];
//var_dump($ordersql);
if($orders['urls']) $mpurl .= '&'.implode('&', $orders['urls']);
$orderby = array($_GET['orderby']=>' selected');
$ordersc = array($_GET['ordersc']=>' selected');

//显示分页
$perpage = empty($_GET['perpage'])?0:intval($_GET['perpage']);
if(!in_array($perpage, array(20,50,100,1000))) $perpage = 20;

$page = empty($_GET['page'])?1:intval($_GET['page']);
if($page<1) $page = 1;
$start = ($page-1)*$perpage;
//检查开始数
ckstart($start, $perpage);

//显示分页
if($perpage > 100) {
	$count = 1;
	$selectsql = 'movieid';
} else {
	$count = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT COUNT(*) FROM ".tname('app_movie')." WHERE $wheresql"), 0);
	$selectsql = '*';
}
$mpurl .= '&perpage='.$perpage;
$perpages = array($perpage => ' selected');
$managebatch = checkperm('managebatch');
$allowbatch = true;
$list = array();
$multi = '';

if($count) {
	$query = $_SGLOBAL['db']->query("SELECT $selectsql FROM ".tname('app_movie')." WHERE $wheresql $ordersql LIMIT $start,$perpage");
	while ($value = $_SGLOBAL['db']->fetch_array($query)) {
		if(!$managebatch && $value['uid'] != $_SGLOBAL['supe_uid']) {
			$allowbatch = false;
		}
		$list[] = $value;
	}
	//var_dump($list);
	$multi = multi($count, $perpage, $page, $mpurl);
}

//显示分页
if($perpage > 100) {
	$count = count($list);
}

//审核控制
if ($_GET['op']==check) {
	//审核通过添加积分：
	
	$statusarr = array('0','1','2');
	if (in_array($_GET['check'],$statusarr)) {
		$checked = $_GET['check']?$_GET['check']:'0';
		$movieid = $_GET['id'];
		$query = $_SGLOBAL['db']->query("SELECT uid,username,mname FROM ".tname('app_movie')." WHERE movieid = '$movieid'");
		$result = $_SGLOBAL['db']->fetch_array($query);
		//var_dump($result);exit();
		$_SGLOBAL['db']->query("UPDATE ".tname('app_movie')." SET checked='$checked' WHERE movieid = '$movieid'");

		if ($checked == 1) {
			$_SGLOBAL['db']->query("UPDATE ".tname('space')." SET credit=credit+$_SGLOBAL[moviecredit] WHERE uid='$result[uid]'");
			
			$query = $_SGLOBAL['db']->query("SELECT uid,username,name FROM ".tname('space')." WHERE uid = '$result[uid]'");
			$spacemovie = $_SGLOBAL['db']->fetch_array($query);
			$result['realname']=$spacemovie['name']?$spacemovie['name']:$spacemovie['username'];
			//事件feed
			$fs = array();
			$fs['icon'] = 'video';
		    $fs['title_template'] = "<a href=\"space.php?uid={$result['uid']}\" target=_blank>{$result['realname']}</a> 发布的新视频 {subject} ，通过审核 (奖励:{$_SGLOBAL[moviecredit]}积分)";
			
			$fs['title_data'] = array(
				'subject' => "<a href=\"space.php?do=movie&id={$movieid}\" target=_blank>{$result['mname']}</a>",
				'score' => $_SGLOBAL[moviecredit]
			);
			$fs['body_template'] = '';
			$fs['id'] = $movieid;
			$fs['idtype'] = 'movieid';
			$fs['body_data'] = array();
			
			include_once(S_ROOT.'./source/function_cp.php');
			$feedid = feeds_add($fs['icon'], $fs['title_template'], $fs['title_data'], $fs['body_template'], $fs['body_data'], $fs['body_general'],$fs['images'], $fs['image_links'], $fs['target_ids'], $fs['friend'],'',1,$result['uid'],$result['realname']);
			if ($feedid&&$movieid) {
				$sql = "UPDATE ".tname('feed')." SET id = ".$movieid.",idtype='movieid' WHERE feedid =".$feedid;
				$_SGLOBAL['db']->query($sql);
			}

				
			//通知
			//var_dump($result['uid']);exit();
			include_once(S_ROOT.'./source/function_cp.php');
			$message = "已审核了您发布的电影 <a href=\"space.php?do=movie&id={$result['uid']}\">{$result['mname']}</a>，已经通过审核！奖励{$moviecredit}积分";
		    notification_add($result['uid'], "app_movie", $message );
		    //更改数量
		    $query = $_SGLOBAL['db']->query("SELECT seid,sendnum FROM ".tname('app_movie_sendusers')." WHERE uid = '$result[uid]'");
			if ($seresult = $_SGLOBAL['db']->fetch_array($query)) {
				$query = $_SGLOBAL['db']->query("UPDATE ".tname('app_movie_sendusers')." SET sendnum=sendnum+1 WHERE seid = '$seresult[seid]'");
			}else {
				inserttable('app_movie_sendusers',array('uid'=>$result[uid],'username'=>$result[username],'sendnum'=>'1'));
			}
		
			
		}
		if ($checked == 2) {
			if ($result['uid'] !=  $_SGLOBAL['supe_uid']) {		
				//通知
				include_once(S_ROOT.'./source/function_cp.php');
				$message = "已审核了您发布的电影 <a href=\"space.php?do=movie&id={$result['uid']}\">{$result['mname']}</a>，未通过审核！请修改！";
			    notification_add($result['uid'], "app_movie", $message );
			}
		}
		cpmessage('satus_movie_success','admincp.php?ac=movie');
		
	}else {
		cpmessage('satus_movie_error','javascript:window.history.go(-1);');
	}
	
}

function feeds_add($icon, $title_template='', $title_data=array(), $body_template='', $body_data=array(), $body_general='', $images=array(), $image_links=array(), $target_ids='', $friend='', $appid='', $returnid=0,$supe_uid=0,$supe_name=0) {
	global $_SGLOBAL;
	
	if(empty($appid)) {
		if(is_numeric($icon)) {
			$appid = 0;
		} else {
			$appid = UC_APPID;//本地
		}
	}
	
	$feedarr = array(
		'appid' => $appid,
		'icon' => $icon,
		'uid' => $supe_uid?$supe_uid:$_SGLOBAL['supe_uid'],
		'username' => $supe_name?$supe_name:$_SGLOBAL['supe_username'],
		'dateline' => $_SGLOBAL['timestamp'],
		'title_template' => $title_template,
		'body_template' => $body_template,
		'body_general' => $body_general,
		'image_1' => empty($images[0])?'':$images[0],
		'image_1_link' => empty($image_links[0])?'':$image_links[0],
		'image_2' => empty($images[1])?'':$images[1],
		'image_2_link' => empty($image_links[1])?'':$image_links[1],
		'image_3' => empty($images[2])?'':$images[2],
		'image_3_link' => empty($image_links[2])?'':$image_links[2],
		'image_4' => empty($images[3])?'':$images[3],
		'image_4_link' => empty($image_links[3])?'':$image_links[3],
		'target_ids' => $target_ids,
		'friend' => $friend,
		'id' => $id,
		'idtype' => $idtype
	);
	
	$feedarr = sstripslashes($feedarr);//去掉转义
	$feedarr['title_data'] = serialize(sstripslashes($title_data));//数组转化
	$feedarr['body_data'] = serialize(sstripslashes($body_data));//数组转化
	$feedarr['hash_template'] = md5($feedarr['title_template']."\t".$feedarr['body_template']);//喜好hash
	$feedarr['hash_data'] = md5($feedarr['title_template']."\t".$feedarr['title_data']."\t".$feedarr['body_template']."\t".$feedarr['body_data']);//合并hash
	$feedarr = saddslashes($feedarr);//增加转义
	
	//去重
	$query = $_SGLOBAL['db']->query("SELECT feedid FROM ".tname('feed')." WHERE uid='$feedarr[uid]' AND hash_data='$feedarr[hash_data]' LIMIT 0,1");
	if($oldfeed = $_SGLOBAL['db']->fetch_array($query)) {
		updatetable('feed', $feedarr, array('feedid'=>$oldfeed['feedid']));
		return 0;
	}
	//var_dump($returnid);exit();
	//插入
	if($returnid) {
		return inserttable('feed', $feedarr, $returnid);
		
	} else {
		inserttable('feed', $feedarr);
		return 1;
	}
}

?>